package com.atguigu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.domain.excel.SubjectData;
import com.atguigu.domain.subject.OneSubject;
import com.atguigu.domain.subject.TwoSubject;
import com.atguigu.listener.SubjectExcelListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.domain.EduSubject;
import com.atguigu.service.EduSubjectService;
import com.atguigu.mapper.EduSubjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
* @author Administrator
* @description 针对表【edu_subject(课程科目)】的数据库操作Service实现
* @createDate 2024-05-17 10:45:33
*/
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject>
    implements EduSubjectService{

    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try{
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();

        }catch (Exception e){
            e.printStackTrace();

        }
    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        List<OneSubject> finalSubject = new ArrayList<>();

        for(int i=0;i<oneSubjectList.size();i++){
            EduSubject eduSubject = oneSubjectList.get(i);
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(eduSubject,oneSubject);

            List<TwoSubject> twoFinalSubject = new ArrayList<>();

            for(int j=0;j<twoSubjectList.size();j++){
                EduSubject tSubject = twoSubjectList.get(j);
                if(tSubject.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    twoFinalSubject.add(twoSubject);
                }

            }

            oneSubject.setChildren(twoFinalSubject);

            finalSubject.add(oneSubject);
        }



        return finalSubject;
    }
}




